package middle;

import util.PrintUtils;

public class Solution_279 {

    public int numSquares(int n) {
        int[] arrays = new int[n + 1];
        int sqrt = (int)Math.sqrt(n);
        for (int i = 1; i <= n; i++) {
            int next = i * i;
            if (i <= sqrt) {
                arrays[next] = 1;
            }
            if (arrays[i] == 0) {
                int min = Integer.MAX_VALUE;
                for (int j = 0; j <= i / 2; j++) {
                    if (arrays[j] > 0 && arrays[i - j] > 0) {
                        int count = arrays[j] + arrays[i - j];
                        if (count < min) {
                            min = count;
                        }
                    }
                }
                arrays[i] = min == Integer.MAX_VALUE ? 0 : min;
            }
        }
        PrintUtils.arrayInt(arrays);
        return arrays[n];
    }

    public static void main(String[] args) {
        Solution_279 model = new Solution_279();
        System.out.println(model.numSquares(1));
        System.out.println(model.numSquares(12));
        System.out.println(model.numSquares(13));
    }
}
